using System;
using System.Web.UI;
using PHSRAG.Insight21.Policy;
using PHSRAG.Utility;
using ER = PHSRAG.Utility.ExceptionReport;

namespace PHSRAG.Insight21
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Settings.GetKeyValue("SingleSignOnEnabled", false))
            {
                string queryCookie = string.Empty;
                string businessUserCookie = string.Empty;

                /*User who is logging into the application or trying to impersonate another user*/
                string userName = Request.Headers["ct-remote-user"];

                /*This section is used to get the user to impersonate
                 and relates to the BusinessUser textbox on the login.asp page*/
                if (Request.Cookies["sys"] != null)
                {
                    queryCookie = Convert.ToString(Request.Cookies["sys"].Value);

                    if (Request.Cookies["businessUser"] != null)
                    {
                        businessUserCookie = Request.Cookies["businessUser"].Value;
                    }
                }


                if (queryCookie.ToLower() == "sysadmin" && businessUserCookie != string.Empty)
                {
                    Login.LoadSysUserContext(userName, businessUserCookie, (ICachePolicy) Session[Names.CachePolicyKey]);
                }
                else
                {
                    Login.LoadUserContext(userName, (ICachePolicy) Session[Names.CachePolicyKey]);
                }
                //Go to the home page
                Response.Redirect("Home/Home.aspx", false);
            }
        }
    }
}